package org.example.resourceplatform.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "doc_type",
        uniqueConstraints = {
                @UniqueConstraint(columnNames = "type_name", name = "uk_type_name")
        })
@Getter
@Setter
@NoArgsConstructor
public class DocType {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "type_id")
    private Integer typeId;

    @NotBlank(message = "类型名称不能为空")
    @Size(max = 20, message = "类型名称长度不能超过20个字符")
    @Column(name = "type_name", nullable = false, unique = true, length = 20)
    private String typeName;

    @Size(max = 100, message = "类型说明长度不能超过100个字符")
    @Column(name = "remark", length = 100)
    private String remark;

    @OneToMany(mappedBy = "docType", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<Document> documents = new ArrayList<>();

    public DocType(String typeName) {
        this.typeName = typeName;
    }

    public DocType(String typeName, String remark) {
        this.typeName = typeName;
        this.remark = remark;
    }

    public void addDocument(Document document) {
        if (documents == null) {
            documents = new ArrayList<>();
        }
        documents.add(document);
        document.setDocType(this);
    }

    public void removeDocument(Document document) {
        if (documents != null) {
            documents.remove(document);
            document.setDocType(null);
        }
    }

    @Override
    public String toString() {
        return "DocType{" +
                "typeId=" + typeId +
                ", typeName='" + typeName + '\'' +
                ", remark='" + remark + '\'' +
                '}';
    }
}